﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using hedefgrup.reports.renault.date;
using hedefgrup.reports.renault.definitions;
using hedefgrup.reports.renault.verbatim;
using System.Text;
using hedefgrup.reports.renault.database;
using System.Collections.Specialized;

public partial class forms_common_verbatimView : reportPage
{
    private void renderQRAVerbatim()
    {
        reportMonth month = base.getReportMonth();

        string str = "SELECT * FROM renault_verbatim WHERE [CARPAN] BETWEEN @p_0 AND @p_1 AND Project = 'QRA' ORDER BY Question ASC";
        int cumulativeMonths = base.getCumulativeMonths();

        object[] args = { month.getMonthYearValue(), (month - (cumulativeMonths - 1)).getMonthYearValue() };

        DataTable table = db.query(str, args);

        StringBuilder contents = new StringBuilder();

        contents.Append("<table cellspacing='0' cellpadding='3' class='maintable'>");
        contents.Append("<tr><th>Bayii</th><th>Soru</th><th>Marka</th><th>Müşteri</th><th>Memnuniyet</th><th>Açık Cevap</th></tr>");

        foreach (DataRow row in table.Rows)
        {
            contents.Append("<tr>");

            dealerDefinition dealer = base.getDataCache().getDealer(row["BAYII"].ToString());
            if (dealer != null)
            {
                contents.Append("<td>" + dealer.getCode() + " - " + dealer.getName() + "</td>");
            }
            else
            {
                contents.Append("<td>" + row["BAYII"].ToString() + "</td>");
            }

            contents.Append("<td>" + row["Question"].ToString() + "</td>");
            contents.Append("<td>" + row["MARKA"].ToString() + "</td>");
            contents.Append("<td>" + row["Interviewee"].ToString() + "</td>");

            contents.Append("<td>" + getSatisfaction(row["Satisfaction"].ToString()) + "</td>");
            contents.Append("<td>" + row["Content"].ToString() + "</td>");
        }

        contents.Append("</table>");

        contents.Append("<br /><br />");
        contents.Append("<div class='navigator'>");
        contents.Append("<a href=\"javascript:history.go(-1);\">Geri Dön</a>&nbsp;&nbsp;&nbsp;&nbsp;</div>");

        verbatimContents.Text = contents.ToString();
    }

    private string getSatisfaction(string satisfaction)
    {
        switch (satisfaction)
        {
            case "1": return "Memnunum";
            case "2": return "Memnun Sayılırım";
            case "3": return "Memnun Değilim";
            case "4": return "Hiç Memnun Değilim";
            default: return satisfaction;
        }
    }

    private string getMakeSelectionPresentationName()
    {
        switch (base.makeSelection)
        {
            case Global.MAKE_DACIA: return "Dacia";
            case Global.MAKE_RENAULT: return "Renault";
            case Global.MAKE_RENAULT_AND_DACIA: return "Toplam";
            case Global.MAKE_RENAULT_COMMERCIAL: return "Renault Ticari";
            case Global.MAKE_RENAULT_PRIVATE: return "Renault Binek";
        }
        return base.makeSelection;
    }

    private string RenderVerbatimSummary()
    {
        dealerDefinition dealer = base.getDealer();
        string questionCode = base.getString("QUESTION");
        string makeSelection = base.makeSelection;

        return string.Format
        (
            "{0} - {5} - Müşteri Yorumları <br/>{1} {2} ({3} aylık) / Marka: {6} / Bayii: {4}",
            base.userProjectName,
            base.getReportMonth().getYear(),
            base.getReportMonth().getMonthName(),
            base.getCumulativeMonths(),
            base.getDealer().getCode() + " - " + base.getDealer().getName(),
            base.getDataCache().questionDefinitions[questionCode].getName(),
            getMakeSelectionPresentationName()
        );
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        base.initialize();
        base.ensureUserIsLoggedIn();

        verbatimSummary.Text = RenderVerbatimSummary();

        if (base.userProjectName == Global.PROJECT_QRA)
        {
            renderQRAVerbatim();
            return;
        }

        reportMonth month = base.getReportMonth();
        dealerDefinition dealer = base.getDealer();

        int startIndex;
        int count;

        if (!int.TryParse(base.getString("START_INDEX"), out startIndex)) startIndex = 1;
        if (!int.TryParse(base.getString("PAGING_ROW_COUNT"), out count)) count = 20;

        string varyByColumn = base.getString("VARYBYCOLUMN");
        string varyByFilter = base.getString("VARYBYFILTER");

        if (!string.IsNullOrEmpty(varyByColumn))
        {
            NameValueCollection varyByDistinctValues = verbatimReportInfo.getVaryByDistinctValuesAndTexts(base.userProjectName, base.makeSelection, varyByColumn);
            foreach (string key in varyByDistinctValues.Keys)
            {
                ListItem distinctValue = new ListItem(varyByDistinctValues[key], key);
                VARYBYFILTER.Items.Add(distinctValue);
                if (varyByFilter == key)
                {
                    distinctValue.Selected = true;
                }
            }
        }
        else
        {
            VARYBYDIV.Visible = false;
        }

        if (string.IsNullOrEmpty(varyByColumn) || string.IsNullOrEmpty(varyByFilter))
        {
            varyByColumn = null;
            varyByFilter = null;
        }

        verbatimReportInfo[] vris = verbatimReportInfo.select(base.userProjectName, base.makeSelection, base.GetVerbatimFilter(), base.getString("QUESTION"), base.getReportMonth(), base.getCumulativeMonths(), base.getDealer(), startIndex, count, varyByColumn, varyByFilter);

        StringBuilder contents = new StringBuilder();

        contents.Append("<table cellspacing='0' cellpadding='3' class='maintable'><tr><th>Cevap Sayısı</th><th>İçerik</th></tr>");
        foreach (verbatimReportInfo vri in vris)
        {
            if (!string.IsNullOrEmpty(vri.Content))
            {
                contents.AppendFormat("<tr><td class='count'>{0}</td><td class='content'>{1}</td></tr>", vri.AnswerCount, vri.Content);
            }
        }
        contents.Append("</table>");

        contents.Append("<br /><br />");
        contents.Append("<div class='navigator'>");
        contents.Append("<a href=\"javascript:history.go(-1);\">Geri Dön</a>&nbsp;&nbsp;&nbsp;&nbsp;");

        if (vris.Length == count)
        {
            contents.AppendFormat
            (
                "<a href=\"?d={0}&m={1}&c={2}&QUESTION={3}&START_INDEX={4}&PAGING_ROW_COUNT={5}\">Sonraki Sayfa</a>",
                dealer.getCode(),
                month.dateValue,
                base.getCumulativeMonths(),
                base.getString("QUESTION"),
                startIndex + count,
                count
            );
        }

        contents.Append("</div>");

        verbatimContents.Text = contents.ToString();


    }
}
